package com.dormitory.service.impl;

import com.dormitory.dao.BuildingDao;
import com.dormitory.po.Building;
import com.dormitory.po.PageInfo;
import com.dormitory.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuildingServiceImpl implements BuildingService {
    @Autowired
    private BuildingDao buildingDao;

//    @Override
//    public Integer totalCount(Integer d_id, String d_dormbuilding, String a_name) {
//        return null;
//    }
//
//    @Override
//    public List<Building> getBuildingList(Integer d_id, String d_dormbuilding, String a_name, Integer currentPage, Integer pageSize) {
//        return null;
//    }


    @Override
    public PageInfo<Building> findPageInfo(String d_dormbuilding, String a_name, Integer pageIndex, Integer pageSize) {
        PageInfo<Building> bi = new PageInfo<Building>();
        bi.setPageIndex(pageIndex);
        bi.setPageSize(pageSize);
        Integer totalCount = buildingDao.totalCount(d_dormbuilding, a_name);
        if (totalCount > 0) {
            bi.setTotalCount(totalCount);
            List<Building> buildingList = buildingDao.getBuildingList(d_dormbuilding, a_name,
                    (bi.getPageIndex() - 1) * bi.getPageSize(), bi.getPageSize());
            bi.setList(buildingList);
        }
        return bi;
    }

    @Override
    public int addBuilding(Building building) {
        return buildingDao.addBuilding(building);
    }

    @Override
    public int deleteBuilding(Integer d_id) {
        return buildingDao.deleteBuilding(d_id);
    }

    @Override
    public int updateBuilding(Building building) {
        return buildingDao.updateBuilding(building);
    }

    @Override
    public Building findBuildingById(Integer d_id) {
        Building b = buildingDao.findBuildingById(d_id);
        return b;
    }

    @Override
    public List<Building> getAll() {
        List<Building> buildingList = buildingDao.getAll();
        return buildingList;
    }
}
